package com.twang.stock.ebay;

import com.alibaba.fastjson.JSONObject;
import com.jayway.jsonpath.JsonPath;
import com.twang.stock.dto.EbayRequest;
import com.twang.stock.dto.StockMinutesMessage;
import com.twang.stock.entity.CompanyStockMinutes;
import com.twang.stock.entity.EbayMerchantInfo;
import com.twang.stock.mapper.EbayMerchantInfoMapper;
import com.twang.stock.redis.RedisUtil;
import com.twang.stock.rocketMq.RocketMQProducer;
import com.twang.stock.threadPool.AsycTaskExecutorConfig;
import com.twang.stock.utils.date.HolidayCopy;
import com.twang.stock.utils.http.HttpStockUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.assertj.core.util.Lists;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;

@Slf4j
@Service
public class EbayService {

    private static  String key_="ebay:EbaySearchByTypeService:";

    private static final Pattern PATTERN = Pattern.compile("/(sch|str)/([^/?]+)");


    @Autowired
    private static RedisUtil redisUtil;
    @Autowired
    private RocketMQProducer rocketMQProducer;

    @Resource
    private EbayMerchantInfoMapper ebayMerchantInfomapper;

    @Resource
    private AsycTaskExecutorConfig asycTaskExecutorConfig;


    @Resource
    private EbayMerchantInfoMapper ebayMerchantInfoMapper;

    public List getList(){
       List<String> list = ebayMerchantInfoMapper.getQueryTypeList();
       return list;
    }

    public List getDetailList(String name){
        EbayMerchantInfo param=new EbayMerchantInfo();
        param.setName(name);
        List<EbayMerchantInfo> list = ebayMerchantInfoMapper.selectByName(param);
        return list;
    }




    /**
     * 获取页面商品对应的店铺
     * @param goodsUrl
     * @param totalPage
     * @throws Exception
     */
    public  void getGoodDetailFeedback(String goodsUrl,int totalPage,String name) throws Exception {
        List<Map<String, Object>> mapList = Lists.newArrayList();
        for (int i = 1; i <= totalPage; i++) {
            String page = "&_pgn=" + i;
            String url = goodsUrl + page;
//            url = "https://www.ebay.com/sch/i.html?_nkw=lining++from+china&_sacat=0&_from=R40&_trksid=p2334524.m570.l1313&_odkw=slo+from+china&_osacat=0" + page;
            String htmlResult = getPage111(url);
            // 网页内容解析
            Document doc = Jsoup.parse(htmlResult);
            // 获取所有 div 元素
            Elements divs = doc.select("div.s-item__image");
            CountDownLatch countDownLatch = new CountDownLatch(divs.size());
            // 遍历每个 div 元素
            int j = 1;
            for (Element div : divs) {
                if (j < 3) {
                    j++;
                    countDownLatch.countDown();
                    continue;
                }
                // 在当前 div 中选择包含 href 属性的 a 元素
                Element link = div.select("a[href]").first();
                if (link != null) {
                    try {
                        // 提取 href 属性的值
                        String href = link.attr("href");
                        // 访问主页，获取detail
                        asycTaskExecutorConfig.taskQueueExecutor().execute(() -> {
                            try {
                                itemUserHomePage(href, name);
                            } catch (Exception e) {
                                log.error(">>>>>>>> Exception itemUserHomePage ", e);
                            } finally {
                                countDownLatch.countDown();
                            }
                        });
                    } catch (Exception e) {
                        log.error(">>>>>>>> Exception ", e);
                    }
                } else {
                    System.out.println("No link found in this div");
                }
            }
            countDownLatch.await();
            log.info(">>>>>>>>>> 采集结果：{}", JSONObject.toJSONString(mapList));
        }
    }

    /**
     * 初始化数据
     * @param name
     * @param href
     */
    public void ebayItemSendToMq(String name, String href) {
        StockMinutesMessage message = new StockMinutesMessage();
        message.setType(StockMinutesMessage.TYPE_ITEM_HOME_PAGE);
        EbayRequest ebayRequest=new EbayRequest<>();
        ebayRequest.setGoodsUrl(href);
        ebayRequest.setName(name);
        message.setData(ebayRequest);
        rocketMQProducer.sendOneWay(JSONObject.toJSONString(message));
    }

    public   boolean itemUserHomePage(String href,String name) {
        Map<String, Object> userMap =new HashMap<>();
        EbayMerchantInfo ebayMerchantInfo=new EbayMerchantInfo();
        ebayMerchantInfo.setName(name);
        log.info(">>>>>>>>>>> 提取出了页面的商品地址 : {}", href);
        //1、访问连接 获取页面的商铺
        ebayMerchantInfo.setHomePage(href);
        try {
            extractedFiandUserINfo(href, userMap, ebayMerchantInfo);
        }catch (Exception e){
            log.error(">>>>>>>>>> 未获取到主页信息");
        }
        //
        if(StringUtils.isNotEmpty(ebayMerchantInfo.getHomePageName())){
            EbayMerchantInfo param=new EbayMerchantInfo();
            param.setHomePageName(ebayMerchantInfo.getHomePageName());
            param.setName(ebayMerchantInfo.getName());
            List<EbayMerchantInfo> list=ebayMerchantInfomapper.select(param);
            if(CollectionUtils.isEmpty(list)){
                ebayMerchantInfomapper.insert(ebayMerchantInfo);
            }
        }else{
            ebayMerchantInfomapper.insert(ebayMerchantInfo);
        }

        return false;
    }

    private static void extractedFiandUserINfo(String href, Map<String, Object> userMap, EbayMerchantInfo ebayMerchantInfo) {
        String indexResult = getPage111(href);
        Document indexPagedoc_ = Jsoup.parse(indexResult);
        //获取feedback
        Elements elements = indexPagedoc_.select("div.x-sellercard-atf__info__about-seller span.ux-textspans--SECONDARY");
        if (!elements.isEmpty()) {
            try {
                String text = elements.get(0).text();
                String number = text.replaceAll("[()]", "");
                // 去掉括号
                log.info("提取的评分是:{} ", number); // 输出: 提取的数字是: 4549
                userMap.put("评分", number);
                ebayMerchantInfo.setScore(Integer.valueOf(number));
            } catch (Exception e) {
                log.error(">>>> 提取的评分 出错 ");
            }

        }
        //获取到用户的主页
        String userHomePage = indexPagedoc_.select("div.x-sellercard-atf__info__about-seller a").attr("href");
        if (!StringUtils.isEmpty(userHomePage)) {
            Boolean istrue = Boolean.FALSE;
            userMap.put("主页地址", userHomePage);

            ebayMerchantInfo.setHomePage(userHomePage);
            try {
                ebayMerchantInfo.setHomePageName(extractUserID(userHomePage));
            } catch (Exception e) {
                log.error(">>>>>>>>>", e);
            }

            //2、获取about页面
            String aboutUrl = new StringBuffer().append(userHomePage.split("\\?")[0]).append("?_tab=about").toString();
            String aboutUrlResult = sendRequest(aboutUrl);
            Document aboutDoc = Jsoup.parse(aboutUrlResult);
            Elements aboutDivs = aboutDoc.select("div.str-seller-card__store-stats-content");
            if (CollectionUtils.isEmpty(aboutDivs)) {
                ebayMerchantInfo.setAboutStatus("F");
                ebayMerchantInfo.setAboutUrl(aboutUrl);
            }
            istrue = 获取好评率and已售出数量(aboutDoc, userMap, ebayMerchantInfo);

            Elements locationDoc = aboutDoc.select("section.str-about-description__seller-info");
            // 提取所在地和注册时间
            istrue = loacationAndregistTime(locationDoc, userMap, ebayMerchantInfo);
            if (!istrue) {
                String a = aboutUrl.replace("sch", "usr").replace("/m.html", "");
                String aboutUrlResult_ = sendRequest(a);
                Document aboutDoc_ = Jsoup.parse(aboutUrlResult_);
                Elements locationDoc_ = aboutDoc_.select("section.str-about-description__seller-info");
                if (locationDoc_.size() < 1) {
                    //丢到mq去消费
                    //  ebayItemSendToMq(name, href);
                    ebayMerchantInfo.setLocalStation("未找到");
                    //return false;
                } else {
                    // 提取所在地和注册时间
                    istrue = loacationAndregistTime(locationDoc_, userMap, ebayMerchantInfo);
                }

            }
        } else {
            log.info(">>>>>>>>>> 未获取到主页信息：userHomePage is null");
        }
    }

    /**
     * 从给定的URL中提取用户ID
     * @param url eBay URL
     * @return 提取的用户ID，或者返回null如果未找到
     */
    public static String extractUserID(String url) {
        Matcher matcher = PATTERN.matcher(url);
        if (matcher.find()) {
            return matcher.group(2);  // 返回匹配到的用户ID
        }
        return null;  // 如果未匹配到用户ID，返回null
    }






    private static Boolean loacationAndregistTime(Elements locationDoc,Map userMap, EbayMerchantInfo ebayMerchantInfo) {
        // 查找所有的 span 元素
        Elements spans = locationDoc.select("section.str-about-description__seller-info span");
        Boolean isTrue = Boolean.FALSE;
        Boolean locationFlag = Boolean.FALSE;
        // 由于信息是顺序排列的，所以可以通过索引直接访问
        if (spans.size() > 0) {
            // 所在地
            Element locationSpan = spans.get(0);
            String location = locationSpan.select("span.BOLD").text();
            Element registrationDateSpan = spans.get(3);
            String registrationDate = registrationDateSpan.select("span.BOLD").text();
            System.out.println("所在地: " + location);
            userMap.put("所在地", location);
            ebayMerchantInfo.setLocalStation(location);
            if (StringUtils.isNotBlank(location)) {
                locationFlag = Boolean.TRUE;
            }
            System.out.println("注册时间: " + registrationDate);
            userMap.put("注册时间", registrationDate);
            ebayMerchantInfo.setRegTime(registrationDate);
            if (StringUtils.isNotBlank(registrationDate)) {
                locationFlag = Boolean.TRUE;
            }
        } else {
            System.out.println("所在地信息未找到");
            ebayMerchantInfo.setLocalStation("所在地信息未找到");
        }
        return locationFlag;
    }

    private static Boolean 获取好评率and已售出数量(Document aboutDoc,Map userMap, EbayMerchantInfo ebayMerchantInfo) {
        Boolean isTrue=Boolean.FALSE;

        // 使用 select 获取所有含有 title 属性的 div 元素
        Elements divsWithTitle = aboutDoc.select("div[title]");

        if (divsWithTitle.size() > 0) {
            // 提取第一个 div 的 title 属性
            Element firstDiv = divsWithTitle.get(0);
            String firstTitle = firstDiv.attr("title");
            System.out.println("First div title: " + firstTitle);
            userMap.put("好评率",firstTitle);
            ebayMerchantInfo.setFeedBackRate(firstTitle);

        } else {
            System.out.println("No div with title found");
        }

        if (divsWithTitle.size() > 1) {
            // 提取第二个 div 的 title 属性
            Element secondDiv = divsWithTitle.get(1);
            String secondTitle = secondDiv.attr("title");
            System.out.println("Second div title: " + secondTitle);
            userMap.put("出售商品数量",secondTitle);
            if(StringUtils.isNotBlank(secondTitle)){
                ebayMerchantInfo.setSoldGoods(secondTitle);
            }
        } else {
            System.out.println("Less than two divs with title found");
        }
        return Boolean.TRUE;
    }

    public synchronized static String getPage111(String urlString) {
        HttpURLConnection connection = null;

        try {
            // 创建 URL 对象
            URL url = new URL(urlString);

            // 打开连接
            connection = (HttpURLConnection) url.openConnection();

            // 设置请求属性 (必须在连接之后设置)
            connection.setRequestProperty("Accept-Charset", "UTF-8"); // 设置请求编码
            // connection.setRequestProperty("Accept", "application/json"); // 可选的请求头

            // 设置请求方法
            connection.setRequestMethod("GET");

            // 获取响应码
            int responseCode = connection.getResponseCode();
            System.out.println("Response Code: " + responseCode);

            String contentType = connection.getContentType();
            System.out.println("查看服务器返回的编码信息 Content-Type: " + contentType); // 查看服务器返回的编码信息

            // 读取响应内容
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // 打印响应内容
            //System.out.println("Response Content: " + response.toString());
            return response.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
        return "";

    }

    public synchronized  static String sendRequest(String urlString) {
        HttpURLConnection connection = null;

        try {
            // 创建 URL 对象
            URL url = new URL(urlString);

            // 打开连接
            connection = (HttpURLConnection) url.openConnection();

            // 设置请求头部，模拟浏览器请求
            connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            connection.setRequestProperty("Accept-Encoding", "gzip, deflate, br");  // 添加支持gzip和deflate编码
            connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
            connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");
            connection.setRequestProperty("Connection", "keep-alive");

            // 设置请求方法
            connection.setRequestMethod("GET");

            // 获取响应码
            int responseCode = connection.getResponseCode();
            System.out.println("Response Code: " + responseCode);

            // 获取 Content-Encoding 头，判断是否有压缩
            String encoding = connection.getHeaderField("Content-Encoding");
            InputStream inputStream = connection.getInputStream();
            if ("gzip".equalsIgnoreCase(encoding)) {
                inputStream = new GZIPInputStream(inputStream);  // 如果是gzip编码，解压
            } else if ("deflate".equalsIgnoreCase(encoding)) {
                inputStream = new java.util.zip.InflaterInputStream(inputStream);  // 如果是deflate编码，解压
            }

            // 获取服务器返回的编码类型
            String contentType = connection.getContentType();
            String charset = "UTF-8"; // 默认使用 UTF-8

            // 从 Content-Type 中提取 charset 参数
            Pattern pattern = Pattern.compile(".*charset=([a-zA-Z0-9-]+).*");
            Matcher matcher = pattern.matcher(contentType);
            if (matcher.find()) {
                charset = matcher.group(1);
            }
            System.out.println("Detected Charset: " + charset);

            // 读取响应内容
            BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, charset));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // 返回响应内容
            return response.toString();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
        return "";
    }


    public static void main(String[] args) {
        String url = "https://www.ebay.com/sch/i.html?_nkw=lining++from+china&_sacat=0&_from=R40&_trksid=p2334524.m570.l1313&_odkw=slo+from+china&_osacat=0";
        String response = getPage111(url);
        System.out.println("Response Content: " + response);
    }


    public static String getHtmlStrByUrlDfcf(String url) {
        // HttpClient 超时配置
        RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD)
                .setConnectionRequestTimeout(6000).setConnectTimeout(6000).build();
        CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig).build();
        // HttpClient 超时配置
        HttpGet httpGetForFName = new HttpGet(url);
        // httpGetForFName.addHeader("User-Agent", USER_AGENT);
        // httpGetForFName.addHeader("Cookie", getCOOKIE());
        httpGetForFName.addHeader("Content-Type", "text/html; charset=utf-8");
        httpGetForFName.addHeader("Accept-Encoding", "gzip, deflate");
        httpGetForFName.addHeader("Connection", "keep-alive");
//        httpGetForFName.addHeader("Referer", "https://q.10jqka.com.cn/");
//        httpGetForFName.addHeader("Host", "q.10jqka.com.cn");

        // 发送请求，并执行
        CloseableHttpResponse response;
        String html = "";
        try {
            response = httpClient.execute(httpGetForFName);
            // response植入cookie
            Header[] ssoResponseHeader = response.getHeaders("Set-Cookie");
            InputStream in = response.getEntity().getContent();
            html = convertStreamToString(in);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return html;
    }

    public static String convertStreamToString(InputStream in) {
        StringBuilder sb = new StringBuilder();
        try {

            InputStreamReader isr = new InputStreamReader(in, "gbk");
            BufferedReader reader = new BufferedReader(isr);
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "/n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();

    }

    public static List<Map<String, String>> parseItems(String content) {
        List<Map<String, String>> results = new ArrayList<>();

        try {
            Document doc = Jsoup.parse(content);
            Elements itemWrappers = doc.select("div.s-item__wrapper.clearfix");

            for (Element wrapper : itemWrappers) {
                Map<String, String> itemInfo = new HashMap<>();

                // 修改点：使用 select().first() 替代 selectFirst()
                Elements locationSpans = wrapper.select("span.s-item__location.s-item__itemLocation");
                if (!locationSpans.isEmpty()) {
                    itemInfo.put("location", locationSpans.first().text());
                }

                // 修改点：使用 select().first() 替代 selectFirst()
                String sellerGoodsInfo = wrapper.select("a.s-item__link").attr("href");
                if (!sellerGoodsInfo.isEmpty()) {
                    itemInfo.put("seller_info_index", sellerGoodsInfo);
                }

                // 同样修改卖家信息提取
                Elements sellerInfoSpans = wrapper.select("span.s-item__seller-info-text");
                if (!sellerInfoSpans.isEmpty()) {
                    String sellerInfoText = sellerInfoSpans.first().text();
                    itemInfo.put("seller_info", sellerInfoText);
                    itemInfo.put("seller_info_score", sellerInfoText.split("\\(")[1].split("\\)")[0].replace(",",""));
                    String username = sellerInfoText.split("\\(")[0];
                    itemInfo.put("seller_info_name", username);
                  //  itemInfo.put("seller_info_index", "https://www.ebay.com/str/" + username);
                }else {
                    continue;
                }
                if (!itemInfo.isEmpty()) {
                    results.add(itemInfo);
                }
            }

        } catch (Exception e) {
            log.error("文件读取错误: " + e.getMessage());
        }
        return results;
    }

    public  void getUserLocationAndFeedback(String content,String name) throws Exception {
        List<Map<String, String>> mapList=parseItems(content);
        List<EbayMerchantInfo> ebayMerchantInfos=Lists.newArrayList();
        if(CollectionUtils.isNotEmpty(mapList)){
            mapList.stream().forEach(map->{
                try{
                    EbayMerchantInfo ebayMerchantInfo =new EbayMerchantInfo();
                    ebayMerchantInfo.setName(name);
                    ebayMerchantInfo.setLocalStation(map.get("location"));
                    ebayMerchantInfo.setHomePage(map.get("seller_info_index"));
                    ebayMerchantInfo.setScore(Integer.valueOf(map.get("seller_info_score")));
                    ebayMerchantInfo.setHomePageName(map.get("seller_info_name"));
                    ebayMerchantInfos.add(ebayMerchantInfo);
                    //ebayMerchantInfoMapper.insert(ebayMerchantInfo);
                }catch (Exception exception){
                    log.error("error -- >",exception);
                }
            });
           int num= ebayMerchantInfoMapper.batchInsert(ebayMerchantInfos);
           log.info(">>>>>name 执行完成，总共生成{} 条",num);
        }
    }




}
